package com.rran.study.chat.ops.repository;

import com.google.common.collect.Maps;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker;

import java.util.Map;

/**
 * @author yy
 * @Type ChannelResponsitory.java
 * @Desc
 * @date 2020/8/6 19:51
 */
public class ChannelResponsitory {
    private static Map<String,ChannelHandlerContext> channelMap =  Maps.newConcurrentMap();
    private static Map<String, WebSocketServerHandshaker> webSocketServerHandshakerMap = Maps.newConcurrentMap();


    public static Map<String,ChannelHandlerContext> getChannelMap(){
        return channelMap;
    }

    public static void removeWebSocketServerHandshaker(String key){
        webSocketServerHandshakerMap.remove(key);
    }
    public static WebSocketServerHandshaker getWebSocketServerHandshaker(String key) {
        return webSocketServerHandshakerMap.get(key);
    }

    public static void storeWebSocketServerHandshaker(String beanId, WebSocketServerHandshaker webSocketServerHandshaker) {
        if (!webSocketServerHandshakerMap.containsKey(beanId)) {
            webSocketServerHandshakerMap.put(beanId, webSocketServerHandshaker);
        }
    }


    public static void removeChannel(String key){
        channelMap.remove(key);
    }

    public static ChannelHandlerContext getChannel(String beanId) {
        return channelMap.get(beanId);
    }

    public static void storeChannel(String beanId, ChannelHandlerContext ctx) {
        if (!channelMap.containsKey(beanId)) {
            channelMap.put(beanId, ctx);
        }
    }

}
